﻿using System;
using System.Collections.Generic;
using Common;
using Common.Entities;
using Common.Models;
using ImpExpData;
using MobileSales.Interface;
using OrderSales;
using Visits;
using System.Windows.Forms;

namespace MobileSales.Presenter
{
    class MainFormPresenter
    {
        private IMainFormView _view;
        private VisitRepository _visits;
        private SplashWindowForm splashForm;

        public MainFormPresenter(IMainFormView view)
        {
            _view = view;
            splashForm = new SplashWindowForm();
            splashForm.Owner = (Form) _view;
            splashForm.Show();
            Application.DoEvents();
            _view.EnableForm = false;
            new SplashFormPresenter(splashForm);
            _view.InitializedComponents += InitializeForm;
            _view.LoadedForm += LoadForm;
        }

        public void InitializeForm(object sender, EventArgs e)
        {
            _visits = DataManager.GetInstance().Visists;
            _view.LoadedForm += LoadForm;
            _view.ChangeDataWork += ChangeDataWork;
            _view.DateWork = DateTime.Now.Date;
            _view.SelectCheckVisitAction += CheckVisit;

            _view.SelectCreateOrderAction += CreateOrder;
            _view.EditOrderAction += EditOrder;
            _view.DeleteOrderAction += DeleteOrder;

            _view.SelectGrapVisitAction += GrapicsVisit;
            _view.SelectSettingsAction += ShowSettings;
            _view.SelectBuildVisitAction += DailyVisits;

            _view.SelectImportDataAction += ImportData;
            _view.SelectExportDataAction += ExportData;

        }

        public void LoadForm(object sender, EventArgs e)
        {
            _view.EnableForm = true;
            _view.VizibleForm = true;
            splashForm.Close();
        }

        public void DailyVisits(object sender, EventArgs e)
        {
           new DailyVisitForm(_view.DateWork).ShowDialog();
            _view.DataSource = _visits.GetVisitsByDateWork(_view.DateWork);
        }

        public void ShowSettings (object sender, EventArgs e)
        {
            new SettingsForm().ShowDialog();
        }

        public void GrapicsVisit (object sender, EventArgs e)
        {
            new SheduleVisitsForm().Show();
        }

        public void ChangeDataWork (object sender, EventArgs e)
        {
            List<Visit> visits = _visits.GetVisitsByDateWork(_view.DateWork);
            _view.DataSource = visits;
        }

        public void CheckVisit (object sender, EventArgs e)
        {
            //try{}
            Visit currentVisit = ((Visit) _view.Current);
            currentVisit.IsVisit = true;
            currentVisit.TimeEndVisit = DateTime.Now.ToString();
            _visits.UpdateVisit(currentVisit);
           // ChangeDataWork(null, null);
        }

        public void CreateOrder (object sender, EventArgs e)
        {
            new OrderForm(DataManager.GetInstance().Orders.CreateOrder(((Visit) _view.Current).IdVisit)).ShowDialog();
            ChangeDataWork(null, null);
        }

        public void EditOrder (object sender, EventArgs e)
        {
            new OrderForm(_view.CurrentOrder).ShowDialog();
            ChangeDataWork(null, null);
        }

        public void DeleteOrder (object sender, EventArgs e)
        {
            DataManager.GetInstance().Orders.DeleteOrder((_view.CurrentOrder).IdOrder);
            ChangeDataWork(null, null);
        }
    
        public void ImportData (object sender, EventArgs e)
        {
            new ImportForm().ShowDialog();
        }

        public void ExportData (object sender, EventArgs e)
        {
            new ExportForm().ShowDialog();
        }
    }
}
